package saucaca.fragment;

import java.util.ArrayList;
import java.util.Map;

import lemon.kenhnhacvn.BaseActivity;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;
import saucaca.Adapter.SongAdapter;
import saucaca.CustomView.XListView;
import saucaca.Object.SongObject;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class LocalSongList extends Fragment {
	public static final String TAG = LocalSongList.class.getSimpleName();
	String sContent;
	int indexPage;
	XListView listview;
	SongAdapter adapter;
	MainActivity mActivity;
	ArrayList<Map<String, Object>> listShow;
	private SwipeRefreshLayout mSwipeLayout;
	// private View mView_EmptyLoading = null;

	public LocalSongList(LocalListFragment localListFragment, String content) {
		sContent = content;
		listShow = MainActivity.getListShow(sContent);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View android = inflater.inflate(R.layout.listview, container, false);
		findviewByID(android);
		addHeader(sContent);
		adapter = new SongAdapter(mActivity, listShow, sContent);
		listview.setAdapter(adapter);
		return android;
	}

	void addHeader(String strKey) {
		View view;
		view = mActivity.getLayoutInflater().inflate(R.layout.playlist_footer_create, null);
		listview.addHeaderView(view);
		Button button = (Button) view.findViewById(R.id.btn_footer);
		if (strKey != null) {
			if (strKey.equals(LocalAllMusic.PLAYLIST_MUSIC)) {
				button.setText("Create New Playlist");
				button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.audio_list_item_addto_default, 0, 0, 0);
				button.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						createNewPlayListEvent();
					}
				});
			} else if (strKey.equals(LocalAllMusic.SONGS_MUSIC)) {
				button.setText("Play All");
				button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.audio_list_item_playlater_default, 0, 0, 0);
				button.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						ArrayList<SongObject> listSongs = new ArrayList<SongObject>();
						for (int i = 0; i < listShow.size(); i++) {
							listSongs.add((SongObject) listShow.get(i).get(BaseActivity.Key_Music));
						}
						((MainActivity) mActivity).SendPlaylistAndplay(listSongs, 0);
						((MainActivity) mActivity).showPanelControl(true);
					}
				});
			} else {
				listview.removeHeaderView(view);
			}

		}
	}

	private void createNewPlayListEvent() {
		int index = 1;
		for (int i = 0; i < BaseActivity.playListMusicMap.size(); i++) {
			if (BaseActivity.playListMusicMap.get(i).get(BaseActivity.Key_Title).equals("playlist" + index)) {
				index++;
				i = 0;
				continue;
			}
		}
		newPlayListDialog(index);
	}

	String namePlaylist = null;

	private void newPlayListDialog(int index) {

		AlertDialog.Builder builder = new Builder(mActivity);
		builder.setTitle("Tạo Playlist");
		final EditText tv = new EditText(mActivity);
		tv.setHint("Playlist name");
		builder.setView(tv);
		builder.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				namePlaylist = tv.getText().toString();
				if (namePlaylist != null) {
					LocalAllMusic.db.creatNewPlayList(namePlaylist);
					BaseActivity.refreshPlayList();
					adapter.setList(BaseActivity.playListMusicMap, LocalAllMusic.PLAYLIST_MUSIC);
				}
			}
		});
		builder.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				namePlaylist = null;
			}
		});
		builder.create().show();
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void findviewByID(View view) {
		listview = (XListView) view.findViewById(R.id.layoutlistview_listview);
		// mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		// listview.setEmptyView(mView_EmptyLoading);
		mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
		mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				BaseActivity.initMediaData();
				listShow = MainActivity.getListShow(sContent);
				adapter.setList(listShow, sContent);
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						mSwipeLayout.setRefreshing(false);
					}
				}, 2000);
			}
		});
		mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
				android.R.color.holo_green_light, android.R.color.holo_orange_light,
				android.R.color.holo_red_light);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (adapter != null && listview != null) {
			adapter.notifyDataSetChanged();
			listview.setAdapter(adapter);
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
		if (adapter != null && listview != null) {
			listview.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		if (adapter != null && listview != null) {
			listview.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		}
	}
}